import type { GetExerciseListParamsI } from '@/types/exercise'
import { cloudDBZone, initCloudDB } from '@/AGC/manager'
import { CloudDBZoneQuery } from '@hw-agconnect/database'
import { ExerciseFilters } from '@/AGC/model/ExerciseFilters'
import { Exercise } from '@/AGC/model/Exercise'
import { ExerciseCollect } from '@/AGC/model/ExerciseCollect'
import type { UserFiltersI } from '@/types/user'

/**
 * 根据运动名称查询运动
 * @param name 运动名称
 * @returns 运动集合
 */
export const queryExerciseListByName = async (name: string) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(Exercise)
  query.contains('name', name)
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 查询运动筛选条件集合--性别&运动能力
 * @param filter 筛选条件
 * @returns 运动筛选条件集合
 */
export const queryExerciseFiltersNotLimit = async (filter: UserFiltersI) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(ExerciseFilters)

  if (filter.sex == 1) {
    query.equalTo('man', 1)
  } else {
    query.equalTo('woman', 1)
  }

  if (filter.sport_ability == 1.375) {
    query.equalTo('never_exercised', 1)
  } else if (filter.sport_ability == 1.55) {
    query.equalTo('exercise_occasionally', 1)
  } else {
    query.equalTo('often_exercised', 1)
  }

  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 查询运动筛选条件--名字&运动能力&分页
 * @param filter 筛选条件
 * @returns 运动筛选条件
 */
export const queryExerciseFilters = async (filter: GetExerciseListParamsI) => {
  if (!cloudDBZone) await initCloudDB()

  const offset = (filter.page - 1) * filter.pageSize
  const query = CloudDBZoneQuery.where(ExerciseFilters)

  if (filter.name) {
    query.contains('name', filter.name)
  }

  if (filter.sport_ability == 1.375) {
    query.equalTo('never_exercised', 1)
  } else if (filter.sport_ability == 1.55) {
    query.equalTo('exercise_occasionally', 1)
  } else if (filter.sport_ability == 1.725) {
    query.equalTo('often_exercised', 1)
  }

  query.limit(filter.pageSize, offset)

  const snapshot = await cloudDBZone.executeQuery(query)

  return snapshot.getSnapshotObjects()
}

/**
 * 查询运动筛选条件总数
 * @param filter 筛选条件
 * @returns 运动筛选条件总数
 */
export const queryExerciseFiltersTotal = async (filter: GetExerciseListParamsI) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(ExerciseFilters)

  if (filter.name) {
    query.contains('name', filter.name)
  }

  if (filter.sport_ability == 1.375) {
    query.equalTo('never_exercised', 1)
  } else if (filter.sport_ability == 1.55) {
    query.equalTo('exercise_occasionally', 1)
  } else if (filter.sport_ability == 1.725) {
    query.equalTo('often_exercised', 1)
  }

  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects().length
}

/**
 * 根据ids查询运动
 * @param ids 运动ids
 * @returns 运动
 */
export const queryExerciseByIds = async (ids: number[]) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(Exercise)
  query.in('id', ids)
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 查询用户收藏的运动
 * @param user_id 用户id
 * @param exercise_id 运动id — 可选
 * @returns 用户收藏的运动
 */
export const queryExerciseCollect = async (user_id: number, exercise_id?: number) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(ExerciseCollect)
  query.equalTo('user_id', user_id)

  if (exercise_id) {
    query.equalTo('exercise_id', exercise_id)
  }

  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 执行用户收藏运动插入
 * @param exerciseCollect 用户收藏运动
 */
export const executeUpsertExerciseCollect = async (exerciseCollect: ExerciseCollect) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeUpsert(exerciseCollect)
  console.log('exerciseCollect-upsert: ' + cloudDBResult)
}

/**
 * 执行用户收藏运动删除
 * @param exerciseCollect 用户收藏运动
 */
export const executeDeleteExerciseCollect = async (exerciseCollects: ExerciseCollect[]) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeDelete(exerciseCollects)
  console.log('exerciseCollect-delete: ' + cloudDBResult)
}
